package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"time"
)

// Note holds the schema definition for the Note entity.
type Note struct {
	ent.Schema
}

// Fields of the Note.
func (Note) Fields() []ent.Field {
	return []ent.Field{
		field.Text("content").
			NotEmpty().
			Comment("内容"),
		field.Int64("from_user_id").
			Comment("发送者ID"),
		field.Int64("to_user_id").
			Comment("接收者ID"),
		field.Bool("is_read").
			Default(false).
			Comment("是否已读"),
		field.Time("deleted_at").
			Optional().
			Nillable().
			Comment("软删除时间"),
		field.Time("created_at").
			Default(time.Now).
			Immutable().
			Comment("创建时间"),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now).
			Comment("更新时间"),
	}
}

// Edges of the Note.
func (Note) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("from_user", User.Type).
			Ref("sent_notes").
			Field("from_user_id").
			Unique().
			Required(),
		edge.From("to_user", User.Type).
			Ref("received_notes").
			Field("to_user_id").
			Unique().
			Required(),
	}
}
